package com.example.demo1109.demo4;

import com.example.demo1109.utli.log;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class Agency implements MethodInterceptor {
    private Object Haha;

    public Agency(Object haha) {
        Haha = haha;
    }

    public Object getInstance(){
        Enhancer enhancer=new Enhancer();
        enhancer.setSuperclass(Haha.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        front(objects);
        Object invoke = method.invoke(Haha, objects);
        System.out.println("结果为"+invoke);
        later(objects,invoke,method.getName());
        return invoke;
    }
    public void front(Object[] sum){
        String a="[0-9]*";
        for (int i = 0; i < sum.length; i++) {
            String b= sum[i].toString();
            boolean flag = b.matches(a);
            if (flag){
                System.out.println("验证第"+(i+1)+"个数字通过");
            }else {
                new Exception("字符不合法");
            }
        }
    }
    public void later(Object[] sum, Object o,Object method){
        if (method.equals("add")){
            log.makeLog(sum[0]+"+"+sum[1]+"="+o);
        }else if (method.equals("cheng")){
            log.makeLog(sum[0]+"*"+sum[1]+"="+o);
        }else if (method.equals("chu")){
            log.makeLog(sum[0]+"/"+sum[1]+"="+o);
        }else if (method.equals("jian")){
            log.makeLog(sum[0]+"-"+sum[1]+"="+o);
        }else {
            System.out.println("日志异常");
        }
    }

}
